{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import plotly.plotly as py\n",
    "from plotly.graph_objs import *\n",
    "from scipy.optimize import curve_fit\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from scipy.stats import norm\n",
    "import plotly.figure_factory as ff\n",
    "import math\n",
    "from scipy.stats import skewnorm\n",
    "import datetime as dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "windVal = []\n",
    "windError = []\n",
    "windOrientation = []\n",
    "prevVal = 20\n",
    "prevOrientation = np.random.uniform(0, 360)\n",
    "for i in range(0, 86400):\n",
    "    windVal.append(abs(np.random.normal(prevVal, 2, 1)[0]))\n",
    "    windError.append(abs(np.random.normal(round(prevVal/10), 1)))\n",
    "    if(i % 100 == 0):\n",
    "        windOrientation.append(np.random.uniform(prevOrientation-50,\n",
    "                                                 prevOrientation+50))\n",
    "    else:\n",
    "        windOrientation.append(np.random.uniform(prevOrientation-5,\n",
    "                                                 prevOrientation+5))\n",
    "    if(round(windVal[-1]) > 45):\n",
    "        prevVal = int(math.floor(windVal[-1]))\n",
    "    elif(round(windVal[-1]) < 10):\n",
    "        prevVal = int(math.ceil(windVal[-1]))\n",
    "    else:\n",
    "        prevVal = int(round(windVal[-1]))\n",
    "    prevOrientation = windOrientation[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame.from_dict({\n",
    "                             'Speed': windVal,\n",
    "                             'SpeedError': windError,\n",
    "                             'Direction': windOrientation\n",
    "                            })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "now = dt.datetime.now()\n",
    "sec = now.second\n",
    "minute = now.minute\n",
    "hour = now.hour\n",
    "\n",
    "totalTime = (hour * 3600) + (minute * 60) + (sec)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sqlite3\n",
    "from datetime import *\n",
    "connex = sqlite3.connect(\"wind-data.db\")  # Opens file if exists, else creates file\n",
    "cur = connex.cursor()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.to_sql(name='Wind', con=connex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>Direction</th>\n",
       "      <th>Speed</th>\n",
       "      <th>SpeedError</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>51818</td>\n",
       "      <td>388.467113</td>\n",
       "      <td>23.455603</td>\n",
       "      <td>2.098050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>51819</td>\n",
       "      <td>385.246230</td>\n",
       "      <td>22.822748</td>\n",
       "      <td>1.073134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>51820</td>\n",
       "      <td>388.454137</td>\n",
       "      <td>21.537013</td>\n",
       "      <td>1.982093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>51821</td>\n",
       "      <td>386.653642</td>\n",
       "      <td>25.422632</td>\n",
       "      <td>2.680533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>51822</td>\n",
       "      <td>387.924312</td>\n",
       "      <td>26.444059</td>\n",
       "      <td>3.757128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>51823</td>\n",
       "      <td>392.384678</td>\n",
       "      <td>24.849731</td>\n",
       "      <td>1.705354</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>51824</td>\n",
       "      <td>388.825201</td>\n",
       "      <td>27.255339</td>\n",
       "      <td>1.626143</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>51825</td>\n",
       "      <td>384.114820</td>\n",
       "      <td>25.504271</td>\n",
       "      <td>1.971804</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>51826</td>\n",
       "      <td>384.646137</td>\n",
       "      <td>23.141819</td>\n",
       "      <td>2.417021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>51827</td>\n",
       "      <td>387.909547</td>\n",
       "      <td>23.208711</td>\n",
       "      <td>1.300878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>51828</td>\n",
       "      <td>388.013444</td>\n",
       "      <td>19.468876</td>\n",
       "      <td>1.846851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>51829</td>\n",
       "      <td>391.431363</td>\n",
       "      <td>16.096508</td>\n",
       "      <td>1.756832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>51830</td>\n",
       "      <td>395.276298</td>\n",
       "      <td>13.434031</td>\n",
       "      <td>0.085637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>51831</td>\n",
       "      <td>391.453616</td>\n",
       "      <td>14.472317</td>\n",
       "      <td>0.229909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>51832</td>\n",
       "      <td>394.933161</td>\n",
       "      <td>9.355090</td>\n",
       "      <td>0.369801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>51833</td>\n",
       "      <td>395.196892</td>\n",
       "      <td>6.285318</td>\n",
       "      <td>1.750981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>51834</td>\n",
       "      <td>396.410848</td>\n",
       "      <td>4.811851</td>\n",
       "      <td>1.339742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>51835</td>\n",
       "      <td>399.557762</td>\n",
       "      <td>2.857893</td>\n",
       "      <td>0.961948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>51836</td>\n",
       "      <td>394.945787</td>\n",
       "      <td>3.804883</td>\n",
       "      <td>1.296398</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>51837</td>\n",
       "      <td>396.697627</td>\n",
       "      <td>3.637859</td>\n",
       "      <td>0.559455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>51838</td>\n",
       "      <td>397.116428</td>\n",
       "      <td>2.725407</td>\n",
       "      <td>0.465070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>51839</td>\n",
       "      <td>395.553973</td>\n",
       "      <td>3.254478</td>\n",
       "      <td>1.126300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>51840</td>\n",
       "      <td>395.357468</td>\n",
       "      <td>6.278269</td>\n",
       "      <td>0.242901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>51841</td>\n",
       "      <td>399.309159</td>\n",
       "      <td>6.217941</td>\n",
       "      <td>1.110519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>51842</td>\n",
       "      <td>396.167154</td>\n",
       "      <td>11.574918</td>\n",
       "      <td>0.472902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>51843</td>\n",
       "      <td>392.946369</td>\n",
       "      <td>12.546761</td>\n",
       "      <td>2.696686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>51844</td>\n",
       "      <td>388.651890</td>\n",
       "      <td>11.567795</td>\n",
       "      <td>0.000096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>51845</td>\n",
       "      <td>389.262298</td>\n",
       "      <td>12.573260</td>\n",
       "      <td>1.361332</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>51846</td>\n",
       "      <td>389.440563</td>\n",
       "      <td>15.219582</td>\n",
       "      <td>0.180667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>51847</td>\n",
       "      <td>390.460858</td>\n",
       "      <td>13.764902</td>\n",
       "      <td>2.724583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>169</th>\n",
       "      <td>51987</td>\n",
       "      <td>423.504998</td>\n",
       "      <td>11.725522</td>\n",
       "      <td>0.794980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>170</th>\n",
       "      <td>51988</td>\n",
       "      <td>423.845203</td>\n",
       "      <td>13.905336</td>\n",
       "      <td>0.421338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>171</th>\n",
       "      <td>51989</td>\n",
       "      <td>423.178823</td>\n",
       "      <td>12.324926</td>\n",
       "      <td>1.188691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>172</th>\n",
       "      <td>51990</td>\n",
       "      <td>425.206527</td>\n",
       "      <td>11.251139</td>\n",
       "      <td>0.113524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>173</th>\n",
       "      <td>51991</td>\n",
       "      <td>423.737817</td>\n",
       "      <td>12.640440</td>\n",
       "      <td>2.880048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>174</th>\n",
       "      <td>51992</td>\n",
       "      <td>418.898456</td>\n",
       "      <td>16.550466</td>\n",
       "      <td>1.918532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>175</th>\n",
       "      <td>51993</td>\n",
       "      <td>417.238872</td>\n",
       "      <td>17.793784</td>\n",
       "      <td>0.551114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>176</th>\n",
       "      <td>51994</td>\n",
       "      <td>415.241132</td>\n",
       "      <td>18.097223</td>\n",
       "      <td>0.654216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>177</th>\n",
       "      <td>51995</td>\n",
       "      <td>417.110020</td>\n",
       "      <td>16.429510</td>\n",
       "      <td>2.152254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>178</th>\n",
       "      <td>51996</td>\n",
       "      <td>421.549616</td>\n",
       "      <td>12.342433</td>\n",
       "      <td>0.132018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>179</th>\n",
       "      <td>51997</td>\n",
       "      <td>422.692404</td>\n",
       "      <td>8.855690</td>\n",
       "      <td>1.373699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>180</th>\n",
       "      <td>51998</td>\n",
       "      <td>419.323263</td>\n",
       "      <td>4.136734</td>\n",
       "      <td>0.133812</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>181</th>\n",
       "      <td>51999</td>\n",
       "      <td>420.651488</td>\n",
       "      <td>7.781654</td>\n",
       "      <td>1.317008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>182</th>\n",
       "      <td>52000</td>\n",
       "      <td>383.283248</td>\n",
       "      <td>8.408271</td>\n",
       "      <td>1.286599</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>183</th>\n",
       "      <td>52001</td>\n",
       "      <td>378.455239</td>\n",
       "      <td>9.031757</td>\n",
       "      <td>0.560556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>184</th>\n",
       "      <td>52002</td>\n",
       "      <td>376.464200</td>\n",
       "      <td>11.943432</td>\n",
       "      <td>0.222717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>185</th>\n",
       "      <td>52003</td>\n",
       "      <td>376.390306</td>\n",
       "      <td>9.969383</td>\n",
       "      <td>0.134329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>186</th>\n",
       "      <td>52004</td>\n",
       "      <td>380.201827</td>\n",
       "      <td>14.736779</td>\n",
       "      <td>1.031438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>187</th>\n",
       "      <td>52005</td>\n",
       "      <td>382.746163</td>\n",
       "      <td>14.692422</td>\n",
       "      <td>1.896312</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>188</th>\n",
       "      <td>52006</td>\n",
       "      <td>386.201541</td>\n",
       "      <td>14.610690</td>\n",
       "      <td>2.840782</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>189</th>\n",
       "      <td>52007</td>\n",
       "      <td>385.191312</td>\n",
       "      <td>16.216857</td>\n",
       "      <td>1.175926</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>190</th>\n",
       "      <td>52008</td>\n",
       "      <td>385.689913</td>\n",
       "      <td>16.272750</td>\n",
       "      <td>0.249718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>191</th>\n",
       "      <td>52009</td>\n",
       "      <td>383.080845</td>\n",
       "      <td>16.021107</td>\n",
       "      <td>0.289782</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>192</th>\n",
       "      <td>52010</td>\n",
       "      <td>385.675010</td>\n",
       "      <td>13.630253</td>\n",
       "      <td>0.994068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>193</th>\n",
       "      <td>52011</td>\n",
       "      <td>382.759256</td>\n",
       "      <td>18.145036</td>\n",
       "      <td>2.438731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>194</th>\n",
       "      <td>52012</td>\n",
       "      <td>383.773835</td>\n",
       "      <td>17.261319</td>\n",
       "      <td>2.498423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>195</th>\n",
       "      <td>52013</td>\n",
       "      <td>385.414001</td>\n",
       "      <td>22.596576</td>\n",
       "      <td>2.913657</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>196</th>\n",
       "      <td>52014</td>\n",
       "      <td>383.930966</td>\n",
       "      <td>25.388647</td>\n",
       "      <td>1.465414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>197</th>\n",
       "      <td>52015</td>\n",
       "      <td>386.898876</td>\n",
       "      <td>23.412001</td>\n",
       "      <td>2.190445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>198</th>\n",
       "      <td>52016</td>\n",
       "      <td>384.857677</td>\n",
       "      <td>23.883922</td>\n",
       "      <td>1.601534</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>199 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     index   Direction      Speed  SpeedError\n",
       "0    51818  388.467113  23.455603    2.098050\n",
       "1    51819  385.246230  22.822748    1.073134\n",
       "2    51820  388.454137  21.537013    1.982093\n",
       "3    51821  386.653642  25.422632    2.680533\n",
       "4    51822  387.924312  26.444059    3.757128\n",
       "5    51823  392.384678  24.849731    1.705354\n",
       "6    51824  388.825201  27.255339    1.626143\n",
       "7    51825  384.114820  25.504271    1.971804\n",
       "8    51826  384.646137  23.141819    2.417021\n",
       "9    51827  387.909547  23.208711    1.300878\n",
       "10   51828  388.013444  19.468876    1.846851\n",
       "11   51829  391.431363  16.096508    1.756832\n",
       "12   51830  395.276298  13.434031    0.085637\n",
       "13   51831  391.453616  14.472317    0.229909\n",
       "14   51832  394.933161   9.355090    0.369801\n",
       "15   51833  395.196892   6.285318    1.750981\n",
       "16   51834  396.410848   4.811851    1.339742\n",
       "17   51835  399.557762   2.857893    0.961948\n",
       "18   51836  394.945787   3.804883    1.296398\n",
       "19   51837  396.697627   3.637859    0.559455\n",
       "20   51838  397.116428   2.725407    0.465070\n",
       "21   51839  395.553973   3.254478    1.126300\n",
       "22   51840  395.357468   6.278269    0.242901\n",
       "23   51841  399.309159   6.217941    1.110519\n",
       "24   51842  396.167154  11.574918    0.472902\n",
       "25   51843  392.946369  12.546761    2.696686\n",
       "26   51844  388.651890  11.567795    0.000096\n",
       "27   51845  389.262298  12.573260    1.361332\n",
       "28   51846  389.440563  15.219582    0.180667\n",
       "29   51847  390.460858  13.764902    2.724583\n",
       "..     ...         ...        ...         ...\n",
       "169  51987  423.504998  11.725522    0.794980\n",
       "170  51988  423.845203  13.905336    0.421338\n",
       "171  51989  423.178823  12.324926    1.188691\n",
       "172  51990  425.206527  11.251139    0.113524\n",
       "173  51991  423.737817  12.640440    2.880048\n",
       "174  51992  418.898456  16.550466    1.918532\n",
       "175  51993  417.238872  17.793784    0.551114\n",
       "176  51994  415.241132  18.097223    0.654216\n",
       "177  51995  417.110020  16.429510    2.152254\n",
       "178  51996  421.549616  12.342433    0.132018\n",
       "179  51997  422.692404   8.855690    1.373699\n",
       "180  51998  419.323263   4.136734    0.133812\n",
       "181  51999  420.651488   7.781654    1.317008\n",
       "182  52000  383.283248   8.408271    1.286599\n",
       "183  52001  378.455239   9.031757    0.560556\n",
       "184  52002  376.464200  11.943432    0.222717\n",
       "185  52003  376.390306   9.969383    0.134329\n",
       "186  52004  380.201827  14.736779    1.031438\n",
       "187  52005  382.746163  14.692422    1.896312\n",
       "188  52006  386.201541  14.610690    2.840782\n",
       "189  52007  385.191312  16.216857    1.175926\n",
       "190  52008  385.689913  16.272750    0.249718\n",
       "191  52009  383.080845  16.021107    0.289782\n",
       "192  52010  385.675010  13.630253    0.994068\n",
       "193  52011  382.759256  18.145036    2.438731\n",
       "194  52012  383.773835  17.261319    2.498423\n",
       "195  52013  385.414001  22.596576    2.913657\n",
       "196  52014  383.930966  25.388647    1.465414\n",
       "197  52015  386.898876  23.412001    2.190445\n",
       "198  52016  384.857677  23.883922    1.601534\n",
       "\n",
       "[199 rows x 4 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con = sqlite3.connect(\"wind-data.db\")\n",
    "df = pd.read_sql_query(\"SELECT * from Wind where rowid > \"+ str(totalTime-200) + \" AND rowid < \" + str(totalTime) + \";\" , con)\n",
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
